//**************************************************
// Written by Nyi Nyi Than - nyinyithann@gmail.com
// Copyright (c) 2010
// License: The MIT License (MIT)
//**************************************************

using System;
using System.Globalization;

namespace Tanker
{
    public static class Guard
    {
        /// <summary>
        /// Checks an argument to ensure it isn't negative.
        /// </summary>
        /// <param name="argumentValue">The argument value to check.</param>
        /// <param name="argumentName">The name of the argument.</param>
        public static  void ArgumentNotNegative(int argumentValue, string argumentName)
        {
            if (argumentValue < 0)
            {
                throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Properties.Resources.InputMustBeNonNegative, argumentName));
            }
        }

        /// <summary>
        /// Checks an argument to ensure it isn't null
        /// </summary>
        /// <param name="argumentValue">The argument value to check.</param>
        /// <param name="argumentName">The name of the argument.</param>
        public static void ArgumentNotNull(object argumentValue, string argumentName)
        {
            if (argumentValue == null)
            {
                throw new ArgumentNullException(argumentName);
            }
        }

        /// <summary>
        /// Checks a string argument to ensure it isn't null or empty
        /// </summary>
        /// <param name="argumentValue">The argument value to check.</param>
        /// <param name="argumentName">The name of the argument.</param>
        public static void ArgumentNotNullOrEmptyOrWhiteSpaceString(string argumentValue, string argumentName)
        {
            ArgumentNotNull(argumentValue, argumentName);

            if (IsEmptyOrWhiteSpace(argumentValue))
            {
                throw new ArgumentException(string.Format(CultureInfo.CurrentCulture,
                                                          Properties.Resources.InputStringMustBeNonEmptyOrWhitespace,
                                                          argumentName));
            }
        }

        private static bool IsEmptyOrWhiteSpace(string value)
        {
            if (value.Length != 0)
            {
                for (int i = 0; i < value.Length; i++)
                {
                    if (!char.IsWhiteSpace(value[i]))
                    {
                        return false;
                    }
                }
            }
            return true;
        }
    }
}